#/bin/sh
echo start build chong phoenix channel=$1,type=$2,branch=$3
. /usr/local/scripts/build_scripts/util.sh
source /etc/profile
echo jenkins var build_user_id=$BUILD_USER_ID
dir_path=`pwd`
customer_name=`cat customer_name|grep customer_options |awk -F "=" '{print $2}'`
if [ "x$customer_name" == "x" ]; then
    echo this is gong ban interface and dont not given any interface
    cp /usr/local/scripts/build_scripts/customer_interfaces/gong_ban_conf ./interface_info
else
    if [ -f /usr/local/scripts/build_scripts/customer_interfaces/$customer_name ]; then
        echo have customer:$customer_name and interfaces as follow:
        cat /usr/local/scripts/build_scripts/customer_interfaces/$customer_name
        cp /usr/local/scripts/build_scripts/customer_interfaces/$customer_name ./interface_info
     else
         echo have no $customer_name interface config and you need add it
         exit 1
      fi
fi
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'
[ -f build_tags ] && sdk_tag=`cat build_tags |grep manifest_tag |awk -F "=" '{print $2}'`
echo sdk_tag=$sdk_tag
if [ "x$sdk_tag" != "x" ]; then
    echo use tag:$sdk_tag to get repo 
    echo "command:repo init -u gerrit:phoenix/manifest -b refs/tags/$sdk_tag"
    repo init -u gerrit:phoenix/manifest -b refs/tags/$sdk_tag
    check_repo tag $sdk_tag
    cd $dir_path
    echo repo config :
    cat .repo/manifest.xml
    repo sync -j4
    repo start $sdk_tag --all
    repo checkout $sdk_tag
    repo branches
    ph_branch_tmp=$sdk_tag
else
    echo use branch $3 to get repo
    echo "command:repo init -u gerrit:phoenix/manifest -b $3"
    repo init -u gerrit:phoenix/manifest -b $3
    check_repo branch $3
    cd $dir_path
    echo repo config :
    cat .repo/manifest.xml
    repo sync -j4
    repo start $3 --all
    repo checkout $3
    repo branches
    ph_branch_tmp=$3
fi 
echo ========manifest branches/tag========= > sdk_info
echo manifest_branch=$3 >> sdk_info
cat build_tags >> sdk_info
echo >> sdk_info
echo ========repos branches========= >> sdk_info
cat .repo/manifest.xml |grep project |awk -F "project " '{print $2}' |awk -F "name=" '{print $1}' |sed s/"\""//g >> sdk_info
echo >> sdk_info
repo_list="SDK SDK/base/upnp SDK/android/sink/sdk-api SDK/android/sink/common SDK/android/sink/SDK-sample"
echo ========repos commit id========= >> sdk_info
for repo_path in $repo_list
do
    cd $dir_path/$repo_path
    echo $repo_path commit id=`git rev-parse HEAD` >> $dir_path/sdk_info
done
cd $dir_path
echo >> sdk_info
echo ========$customer_name build config========= >> sdk_info
cat interface_info >> sdk_info

du -sh 
cp sdk_config SDK/
cp interface_info SDK/
cd SDK/

trigger_user=$BUILD_USER_ID
phoenix_channel=$1
phoenix_type=$2
sdk_path=`pwd`
echo sdk now path=$sdk_path

[ "x$BUILD_USER_ID" == "x" ] && trigger_user=$BUILD_USER
[ "x$BUILD_USER" == "x" ] && trigger_user=Timer
echo user $trigger_user trigger this build

now_time=`date "+%Y%m%d%H%M"`
commit=`git rev-parse --short HEAD`
ph_branch=`echo $ph_branch_tmp |sed 's/\.//g'` #删除分支名或者tag中的点符号
now_time="$now_time"_"$ph_branch"_"$commit"_"$trigger_user"_"$phoenix_channel"_"$phoenix_type"
echo ftp dir name is $now_time

[ -f gradlew ] && chmod 777 gradlew
cp /usr/local/scripts/build_scripts/local.properties android/sink/
ph_ftp_dir=phoenix_sdk

chmod 777 build.sh
git status
git diff
echo "build_channle=$phoenix_channel"
if  [ -f feature.sh ]; then 
    echo interface_info as follow:
    cat interface_info
    build_options_tmp=`cat interface_info |grep "build_option:" |awk -F ":" '{print $2}' |sed s/LEBO_OPTION_[0-9]=//g|sed s/"OPTION_USE_LELINKPLAYER="//g`
    if [ "x`echo $build_options_tmp |grep LEBO_OPTION_`" != "x" ]; then #不为空，说明参数变量后缀是两位数
        echo $build_options_tmp  need more deal with
        build_options=`echo $build_options_tmp |sed s/LEBO_OPTION_[0-9][0-9]=//g`
    else
        build_options=$build_options_tmp
    fi
    build_actions_tmp=`cat interface_info |grep "build_action:" |awk -F ":" '{print $2}' |sed s/LEBO_ACTION_[0-9]=//g|sed s/LBEO_ACTION_[0-9]=//g`
     if [ "x`echo $build_actions_tmp |grep LEBO_ACTION_`" != "x" ]; then #不为空，说明参数变量后缀是两位数
         echo $build_options_tmp  need more deal with
         build_actions=`echo $build_actions_tmp |sed s/LEBO_ACTION_[0-9][0-9]=//g`
     else
         build_actions=$build_actions_tmp
     fi
    echo build_options is $build_options and build_actions is $build_actions
    [ "x$build_options" == "x" ] && build_interfaces="$build_actions" && echo actions:$build_interfaces 
    [ "x$build_actions" == "x" ] && build_interfaces="$build_options" && echo options:$build_interfaces
    [ "x$build_options" != "x"  -a "x$build_actions" != "x" ] && build_interfaces="$build_options,$build_actions" && echo all: $build_interfaces
    echo build_interfaces is $build_interfaces
    if [ "x`echo $build_interfaces |grep LEBO_`" != "x" -o "x`echo $build_interfaces |grep =`" != "x" ]; then #不为空,说明没处理正确
        echo $build_interfaces also have illeage string, so exit
        exit 1
    fi
    echo "deal with feature.sh (run:dos2unix and chmod 777) and run it "
    dos2unix feature.sh android/sink/feature.sh
    chmod 777 feature.sh android/sink/feature.sh
fi
sed -i 's/,/\n/g' sdk_config
echo sdk config as follow:
cat sdk_config
have_ijk=`cat sdk_config |grep have_ijk |awk -F "=" '{print $2}'`
have_custom=`cat sdk_config |grep have_custom |awk -F "=" '{print $2}'`
have_free=`cat sdk_config |grep have_free |awk -F "=" '{print $2}'`
player_ui=`cat sdk_config |grep player_ui |awk -F "=" '{print $2}'`
have_domain=`cat sdk_config |grep have_domain |awk -F "=" '{print $2}'`
update_zip=`cat sdk_config |grep update_zip |awk -F "=" '{print $2}'`
have_conference=`cat sdk_config |grep have_conference |awk -F "=" '{print $2}'`
all_option=`cat sdk_config |grep all_option |awk -F "=" '{print $2}'`
mirrorCloud=`cat sdk_config |grep mirrorCloud |awk -F "=" '{print $2}'`
mirrorUsb=`cat sdk_config |grep mirrorUsb |awk -F "=" '{print $2}'`
aijiamiFile=`cat sdk_config |grep aijiamiFile |awk -F "=" '{print $2}'`
internation=`cat sdk_config |grep internation |awk -F "=" '{print $2}'`
# 参数需要为key value 形式，可参考如下 base 目录下的 build.sh
#channel:leboapk,buildtype:release,demoName:demo,onlybuild:0,ijk:1,free:1,playerUI:1,bangcleFile:2,domain:0
# channel:     渠道，默认是happytest
# buildtype:   release or debug，默认是debug
# demoName:    demo名称，demo,demo-raw,demo-player,demo-jar,demo-ding,demo-option 默认是demo
# onlybuild:   1 build&install ; 0 build only，默认是1
# ijk:         是否需要ijk 1需要，0不需要，2不需要liblbffmpeg.so(满足sdk包尽量小的需求) 默认1
# free:        是否是免费版本sdk    1 免费，0不免费。默认1
# playerUI:    是否使用乐播播放UI   1 是，0否。默认1
# bangcleFile: 加固策略   0都不加固，1加固so及aar， 2仅加固so， 3仅加固aar，默认2
# domain:      域名配置   0乐播域名，1 CIBN域名   默认0
# dlna: dlna开关生效值配置，1表示默认的dlna开关生效值配置（0(关)/1(开)），2 表示修改dlna生效开关值（8888(关)/9999(开)）
# updateZip: 升级包制作，0 默认只有一个升级包，大于0 打包多个升级包（当前api版本递减），默认0
# conference: 是否是conference版本,1是，0不是。默认0
# mirrorCloud 云镜像功能 0不含云镜像 ， 1移动云镜像。 默认0
# mirrorUsb usb镜像 0不含usb镜像功能，1包含ubs镜像功能。 默认0
# aijiamiFile: 文件加固策略   0都不加固， 1加固指定so及aar， 2仅加固指定so， 3仅加固aar，默认0
# internation 国际化 0中英繁  1中英繁+其它 默认 0
if [ "$update_zip" != "0" ]; then #热升级包需要修改版本号
    vcode_old=`sed -n 42p android/sink/build.gradle |awk -F ":" '{print $2}' |awk -F "," '{print $1}'`
    vcode_new=`expr $vcode_old + 100 ` #老版本号+100，例如18902变成19002
    vname_new=`echo "${vcode_new:0:1}.${vcode_new:1:2}.${vcode_new:3:4}"` #例如18902变成1.89.02

    echo hotfix modify version to versionCode:$vcode_new and versionName:$vname_new
    sed -i "42c versionCode: $vcode_new\," android/sink/build.gradle #整行替换
    sed -i "43c versionName: \"$vname_new\"\," android/sink/build.gradle
    sed -i "2c VERSION := '$vcode_new'" base/Android.mk

    echo check result: #显示对应行内容
    sed -n 42p android/sink/build.gradle
    sed -n 43p android/sink/build.gradle
    sed -n 2p  base/Android.mk
fi
#如果demoName是demo-option时，所有接口都要打开，即不执行feature.sh即可
if [ "$have_custom" == "demo-option" -o "$all_option" == "1" ];then
    echo need open all interfaces and not run feature.sh
else
    echo this is not demo-option and run feature.sh
    echo build_interfaces is $build_interfaces
    echo "sh feature.sh $build_interfaces"
    sh feature.sh "$build_interfaces"
fi
#如果demoName是demo-conference,即会议室方案，必须为收费版
[ "$have_custom" == "demo-conference" ] && have_free=0  && have_conference=1 
build_var="channel:$phoenix_channel,buildtype:$phoenix_type,demoName:$have_custom,onlybuild:0,ijk:$have_ijk,free:$have_free,playerUI:$player_ui,domain:$have_domain,updateZip:$update_zip,conference:$have_conference,bangcleFile:0,mirrorCloud:$mirrorCloud,mirrorUsb:$mirrorUsb,aijiamiFile:$aijiamiFile,internation:$internation"
echo "command:sh build.sh $build_var" | tee -a $dir_path/sdk_info
echo feature.dat valid_interface=`cat android/sink/sdk-api/src/main/assets/hpplay/dat/feature.dat` >> $dir_path/sdk_info
sh build.sh $build_var |tee build_result

get_build_result #判断是否打包成功

[ "$phoenix_type" == "release" ]  &&  cp android/sink/sdk-api/libs target/ -rn  #release的接收端sdk要回收libs
cp $dir_path/sdk_info target
cp target target_all -rn
mv target target_tmp
mkdir target
mv  target_tmp/*.tar  target_tmp/*.aar  target_tmp/*.jar  target/
#如果demo不是demo-option,需要把demo apk放到public中，否则放在public外面（因为demo-option 是对内的，不能给外面）
[ "$have_custom" != "demo-option" ] && mv target_tmp/*.apk target/
upload_file $ph_ftp_dir/$now_time public
cd $sdk_path
rm target -rf
mv target_tmp target

upload_file $ph_ftp_dir $now_time  #上传文件到ftp
